﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
/// <summary>
/// 代码相关扩展
/// </summary>
public static class ESF_CodeExtends
{
    /// <summary>
    /// HF标记
    /// </summary>
    const string mHFSign = @"HF";

    /// <summary>
    /// ESF
    /// </summary>
    const string mESFSign = "ESF";
    /// <summary>
    /// SF
    /// </summary>
    const string mSFSign = "SF";

    /// <summary>
    /// ESF标记正则
    /// </summary>
    readonly static string mESFSignRegex = @$"(_+?{mESFSign}_+?)|(_+?{mESFSign})|({mESFSign}_+?)";
    /// <summary>
    /// SF标记正则
    /// </summary>
    readonly static string mSFSignRegex = @$"(_+?{mSFSign}_+?)|(_+?{mSFSign})|({mSFSign}_+?)";
       

    /// <summary>
    /// 导出标记映射
    /// </summary>
    readonly static Dictionary<string,string> mExtractSignMaping = new Dictionary<string, string>() {
        { mESFSign,mESFSignRegex},
        { mSFSign,mSFSignRegex},
    };

    /// <summary>
    /// 替换文本中所有ESF标记为HF
    /// </summary>
    /// <param name="_text">文本</param>
    /// <returns>文本</returns>
    public static string ReplaceSignESF2HF(this string _text)
    {
        return OnReplaceSign2X(_text, mESFSign, mESFSignRegex, mHFSign);
    }

    /// <summary>
    /// 替换导出标记为替换文本
    /// </summary>
    /// <param name="_text">文本</param>
    /// <param name="_replace">替换文本</param>
    /// <returns>文本</returns>
    public static string ReplaceExtractSign2X(this string _text, string _replace = mHFSign)
    {
        foreach (var key in mExtractSignMaping)
        {
            _text = OnReplaceSign2X(_text, key.Key, key.Value, _replace);
        }
        return _text;
    }

    /// <summary>
    /// 替换字符
    /// </summary>
    /// <param name="_text">文本</param>
    /// <param name="_sign">标记</param>
    /// <param name="_replaceRegex">替换正则</param>    
    /// <param name="_replace">替换字符</param>
    /// <returns>替换后字符</returns>
    static string OnReplaceSign2X(this string _text,string _sign, string _replaceRegex, string _replace)
    {        
        _text = string.IsNullOrEmpty(_text) ? _text : Regex.Replace(_text, _replaceRegex, _replace);
        _text = string.IsNullOrEmpty(_text) ? _text : Regex.Replace(_text, _sign, _replace);
        return _text;
    }
}
#endif
